/*************************************************************************************/
/*  HTML5-Canvas-Presentation & Editor (create presentations with a clear structure) */
/*  Copyright (C) 2012  Michael Fürst                                                */
/*                                                                                   */
/*  This program is free software; you can redistribute it and/or modify it          */
/*  under the terms of the GNU General Public License as published by the            */
/*  Free Software Foundation; either version 3 of the License, or (at your           */
/*  option) any later version.                                                       */
/*                                                                                   */
/*  This program is distributed in the hope that it will be useful, but              */
/*  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY       */
/*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License          */
/*  for more details.                                                                */
/*                                                                                   */
/*  You should have received a copy of the GNU General Public License along          */
/*  with this program; if not, see <http:/* www.gnu.org/licenses/>.                  */
/*************************************************************************************/

/* JSLinker class */
var JSLinker        = new Object();
JSLinker.render_func = new Function();
JSLinker.background_func = new Function();

JSLinker.background = "\n\tStyleLib.background(step, percent, theme.background);";
JSLinker.init       = "\n\tPresentation.appendPath(0,0,1,0);\n";
JSLinker.render     = "\n\tPresentation.colorRGBA(theme.red, theme.green, theme.blue, theme.alpha);\n\tPresentation.text(0,0,'Hello World',50,0);\n";
JSLinker.more       = "";
JSLinker.init_more  = function () {
JSLinker.more += "/*************************************************************************************/\n";
JSLinker.more += "/*  HTML5-Canvas-Presentation & Editor (create presentations with a clear structure) */\n";
JSLinker.more += "/*  Copyright (C) 2012  Michael Fürst                                                */\n";
JSLinker.more += "/*                                                                                   */\n";
JSLinker.more += "/*  This program is free software; you can redistribute it and/or modify it          */\n";
JSLinker.more += "/*  under the terms of the GNU General Public License as published by the            */\n";
JSLinker.more += "/*  Free Software Foundation; either version 3 of the License, or (at your           */\n";
JSLinker.more += "/*  option) any later version.                                                       */\n";
JSLinker.more += "/*                                                                                   */\n";
JSLinker.more += "/*  This program is distributed in the hope that it will be useful, but              */\n";
JSLinker.more += "/*  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY       */\n";
JSLinker.more += "/*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License          */\n";
JSLinker.more += "/*  for more details.                                                                */\n";
JSLinker.more += "/*                                                                                   */\n";
JSLinker.more += "/*  You should have received a copy of the GNU General Public License along          */\n";
JSLinker.more += "/*  with this program; if not, see <http:/* www.gnu.org/licenses/>.                  */\n";
JSLinker.more += "/*************************************************************************************/\n";
JSLinker.more += "\n";
JSLinker.more += "var data_width = "+data_width+";\n";
JSLinker.more += "var data_height = "+data_height+";\n";
JSLinker.more += "var theme = StyleLib.themes.clean;";
};

JSLinker.marker     = 0;

JSLinker.toFile     = function() {
	var file = "";
	if (JSLinker.more == "") {
		JSLinker.init_more();
	}
	file += JSLinker.more;
	file += "\n";
	file += "\n";
	file += "function data_background(step, percent) {";
	file += JSLinker.background;
	file += "}\n";
	file += "\n";
	file += "function data_init() {";
	file += JSLinker.init;
	file += "}\n";
	file += "\n";
	file += "function data_render() {";
	file += JSLinker.render;
	file += "}";
	

	document.getElementById("code").value = file;
};
JSLinker.getCodeSegment = function(pointer, file) {
	var start = true;
	var inc = 0;
	marker = pointer;
	while (inc > 0 || start == true) {
		marker++;
		if (file.length <= marker) return "alert('ERROR');";
		if (file[marker] == "{") {
			inc++;
			if (start == true) {
				start = false;
				pointer = marker+1;
			}
		}
		if (file[marker] == "}") {
			inc--;
		}
	}
	return file.substring(pointer, marker);
};
JSLinker.removeCodeSegment = function(pointer, file) {
	return file.substring(0, pointer)+file.substring(marker+1, file.length);
};

JSLinker.removeWhitespaceOverhead = function(file) {
	while(file[file.length-1] == "\n") {
		file = file.substring(0, file.length-1);
	}
	return file;
};
JSLinker.getParams = function(pointer, code) {
	var start = true;
	var inc = 0;
	var pos = 0;
	var params = [];
	marker = pointer;
	while (inc > 0 || start == true) {
		marker++;
		if (code.length <= marker) return "alert('ERROR');";
		if (code[marker] == "(") {
			inc++;
			if (start == true) {
				start = false;
				pointer = marker+1;
			}
		}
		if (code[marker] == ")") {
			inc--;
			if (inc == 0) {
				params[pos] = code.substring(pointer, marker);
				pos++;
				pointer = marker+1;
			}
		}
		if (code[marker] == "," && inc == 1) {
			params[pos] = code.substring(pointer, marker);
			pos++;
			pointer = marker+1;
		}
	}
	return params;
};
JSLinker.fromFile = function() {
	var file = document.getElementById("code").value;
	var pointer = file.indexOf("function data_background");
	background = JSLinker.background = JSLinker.getCodeSegment(pointer, file);
	file = JSLinker.removeCodeSegment(pointer, file);
	pointer = file.indexOf("function data_init");
	JSLinker.init = JSLinker.getCodeSegment(pointer, file);
	file = JSLinker.removeCodeSegment(pointer, file);
	pointer = file.indexOf("function data_render");
	render = JSLinker.render = JSLinker.getCodeSegment(pointer, file);
	file = JSLinker.removeCodeSegment(pointer, file);
	JSLinker.more = JSLinker.removeWhitespaceOverhead(file);
	eval.call(window, file);
	Helper.clear();
	Presentation.clearPath();
	eval(JSLinker.init);
	JSLinker.render_func = new Function(JSLinker.render);
	JSLinker.background_func = new Function("step", "percent", JSLinker.background);
	JSLinker.toFile();
	Presentation.debug(false);
	window.setTimeout(JSLinker.startParser, 100);
};

JSLinker.startParser = function () {
	var file = document.getElementById("code").value;
	JSLinker.parseJS(JSLinker.more, file);	
	JSLinker.parseJS(JSLinker.init, file);
	JSLinker.parseJS(JSLinker.render, file);
	JSLinker.parseJS(JSLinker.background, file);
	Presentation.debug(true);
};

JSLinker.parseJS = function(code, file) {
	if (enableGUI_parser == false) return;
	var lines = code.split(";");
	for(var i = 0; i < lines.length; i++) {
		var pointer = 0;
		pointer = lines[i].indexOf("Presentation.");
		if (pointer >= 0) {
			var params = JSLinker.getParams(pointer, lines[i]);
			if (params.length == 0) continue;
			var type = lines[i].substring(pointer+13, lines[i].indexOf(params[0])-1);
			switch (type) {
			/* get the text into the data structure */
			case "text": Helper.pushText(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4]); break;
			case "textA": Helper.pushTextA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]); break;
			case "textAE": Helper.pushTextAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]); break;
			case "textE": Helper.pushTextE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6]); break;

			/* get the text into the data structure */
			case "box": Helper.pushBox(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5]); break;
			case "boxA": Helper.pushBoxA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10], params[11]); break;
			case "boxAE": Helper.pushBoxAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10], params[11]); break;
			case "boxE": Helper.pushBoxE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7]); break;

			/* get the text into the data structure */
			case "line": Helper.pushLine(file.indexOf(lines[i]), params[0], params[1], params[2], params[3]); break;
			case "lineA": Helper.pushLineA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9]); break;
			case "lineAE": Helper.pushLineAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9]); break;
			case "lineE": Helper.pushLineE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5]); break;

			/* get the text into the data structure */
			case "circle": Helper.pushCircle(file.indexOf(lines[i]), params[0], params[1], params[2]); break;
			case "circleA": Helper.pushCircleA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7]); break;
			case "circleAE": Helper.pushCircleAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7]); break;
			case "circleE": Helper.pushCircleE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4]); break;

			/* get the text into the data structure */
			case "img": Helper.pushImg(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4]); break;
			case "imgE": Helper.pushImgE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6]); break;
			case "imgP": Helper.pushImgP(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5]); break;
			case "imgPE": Helper.pushImgPE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7]); break;
			case "imgPA": Helper.pushImgPA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]); break;
			case "imgPAE": Helper.pushImgPAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]);break;
			case "imgS": Helper.pushImgP(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6]); break;
			case "imgSE": Helper.pushImgP(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8]);  break;
			case "imgSA": Helper.pushImgSA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10], params[11], params[12]); break;
			case "imgSAE": Helper.pushImgSAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10], params[11], params[12]); break;

			/* get the text into the data structure */
			case "vid": Helper.pushVid(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4]); break;
			case "vidE": Helper.pushVidE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6]); break;
			case "vidP": Helper.pushVidP(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5]); break;
			case "vidPE": Helper.pushVidPE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7]); break;
			case "vidPA": Helper.pushVidPA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]); break;
			case "vidPAE": Helper.pushVidPAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]);break;
			case "vidS": Helper.pushVidP(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6]); break;
			case "vidSE": Helper.pushVidP(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8]);  break;
			case "vidSA": Helper.pushVidSA(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10], params[11], params[12]); break;
			case "vidSAE": Helper.pushVidSAE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10], params[11], params[12]); break;

			/* get the text into the data structure */
			case "audio": break;

			/* get the text into the data structure */
			case "loadImage": break;
			case "loadVideo": break;
			case "loadAudio": break;

			/* get the text into the data structure */
			case "appendPath": Helper.pushPath(file.indexOf(lines[i]), params[0], params[1], params[2], params[3]); break;
			case "appendPathE": Helper.pushPathE(file.indexOf(lines[i]), params[0], params[1], params[2], params[3], params[4]); break;
			default: /* no default */ break;
			}
		}
	}
};

/* EXPORTS */
background = JSLinker.background_func;
render = JSLinker.render_func;
